
package cn.itcast.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.RoleRepository;
import cn.itcast.bos.dao.system.UserRepository;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.RoleService;
import cn.itcast.bos.service.system.UserService;
@Service
@Transactional
public class UserServiceimpl implements UserService {
  @Autowired
  private UserRepository userRepository;
  
  @Autowired
  private RoleRepository roleRepository;
	@Override
	public User findByUsername(String username) {
		// TODO Auto-generated method stub
		return userRepository.findByUsername(username);
	}
	@Override
	public List<User> findAll() {
		return userRepository.findAll();
	}
	@Override
	public void save(User user, String[] roleIds) {
		System.out.println("授予用户的角色的id"+roleIds);
		//保存用户
		userRepository.save(user);
	     if(roleIds !=null){
	    	 for (String roleId : roleIds) {
	    		 Role role = roleRepository.findOne(Integer.parseInt(roleId));
	    		 user.getRoles().add(role);
			}
	     }
	 	
	}

}
